﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Talentnet.Data.Entity;
using Talentnet.Core.Utilities.Mail;
using Talentnet.Data.Access;
using System.Collections.Generic;

/// <summary>
/// Summary description for SessionManager
/// </summary>
public static class SessionManager
{
    public static string[] CurrentUserPermissions
    {
        get
        {
            if (HttpContext.Current.Session["UserPermissions"] != null)
            {
                return (string[])HttpContext.Current.Session["UserPermissions"];
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["UserPermissions"] = value;
        }
    }
    public static LoginUser CurrentUser
    {
        get
        {
            if (HttpContext.Current.Session["CurrentUser"] != null)
            {
                return (LoginUser)HttpContext.Current.Session["CurrentUser"];
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["CurrentUser"] = value;
        }
    }
    public static DataTable CurrentUserMenus
    {
        get
        {
            if (HttpContext.Current.Session["CurrentUserMenus"] != null)
            {
                return (DataTable)HttpContext.Current.Session["CurrentUserMenus"];
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["CurrentUserMenus"] = value;
        }
    }
    public static SiteEnums.UserType CurrentUserType
    {
        get
        {
            if (HttpContext.Current.Session["CurrentUserType"] != null)
            {
                return (SiteEnums.UserType)HttpContext.Current.Session["CurrentUserType"];
            }
            return SiteEnums.UserType.Unknown;
        }
        set
        {
            HttpContext.Current.Session["CurrentUserType"] = value;
        }
    }
    public static bool ResetPwd
    {
        get
        {
            if (HttpContext.Current.Session["ResetPwd"] != null)
            {
                return (bool)HttpContext.Current.Session["ResetPwd"];
            }
            return false;
        }
        set
        {
            HttpContext.Current.Session["ResetPwd"] = value;
        }
    }
    public static bool PwdExpired
    {
        get
        {
            if (HttpContext.Current.Session["PwdExpired"] != null)
            {
                return (bool)HttpContext.Current.Session["PwdExpired"];
            }
            return false;
        }
        set
        {
            HttpContext.Current.Session["PwdExpired"] = value;
        }
    }
    public static bool PwdNearlyExpired
    {
        get
        {
            if (HttpContext.Current.Session["PwdNearlyExpired"] != null)
            {
                return (bool)HttpContext.Current.Session["PwdNearlyExpired"];
            }
            return false;
        }
        set
        {
            HttpContext.Current.Session["PwdNearlyExpired"] = value;
        }
    }

    /// <summary>
    /// Get Mail Setting
    /// </summary>
    public static MailServiceSettings MailSetting
    {
        get
        {
            if (HttpContext.Current.Session["MailSetting"] == null)
            {
                MailServiceSettings mailSetting = new MailServiceSettings();
                mailSetting.AuthenticationType = SmtpAuthentication.Basic;

                List<REF_Config> clientInfo = SessionManager.ClientSettings;
                foreach (REF_Config client in clientInfo)
                {
                    switch (client.Key)
                    {
                        case "SMTPServer":
                            mailSetting.Server = client.Value;
                            break;
                        case "SMTPUserName":
                            mailSetting.Username = client.Value;
                            break;
                        case "SMTPPassword":
                            mailSetting.Password = client.Value;
                            break;
                        case "SMTPPort":
                            mailSetting.Port = Convert.ToInt32(client.Value);
                            break;
                        case "SMTPSSL":
                            mailSetting.EnableSSL = client.Value.Equals("1");
                            break;
                        case "EmailContact":
                            mailSetting.EmailContact = client.Value;
                            break;
                    }

                }
                HttpContext.Current.Session["MailSetting"] = mailSetting;
            }
            return (MailServiceSettings)HttpContext.Current.Session["MailSetting"];
        }
    }

    /// <summary>
    /// Get ClientSettings
    /// </summary>
    public static List<REF_Config> ClientSettings
    {
        get
        {
            int clientID = SiteConstants.Talentnet.CLIENT_ID;
            if (SessionManager.CurrentUser != null)
                clientID = SessionManager.CurrentUser.ClientID;
            List<REF_Config> _ClientSettings = new List<REF_Config>();
            if (HttpContext.Current.Session["ClientSettings"] == null)
            {
                _ClientSettings = DatabaseManager.TalentnetDB.REF_Configs.Where(c => c.ClientId == clientID).Select(c => c).ToList();
                DatabaseManager.TalentnetDB.SubmitChanges();
                HttpContext.Current.Session["ClientSettings"] = _ClientSettings;
            }
            return (List<REF_Config>)HttpContext.Current.Session["ClientSettings"];
        }
    }
    
}
